// pages/channel/apply/apply.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    apply_id: '', // 渠道申请id
    name: '', // 单位名称
    contact: '', // 联系人
    tellphone: '', // 联系电话
    brief: '', // 简介
    enclosure: [], // 附件
    isShow: false,
    qi_niu_url: app.globalData.qi_niu_url,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.is_again == 1) {
      wx.setNavigationBarTitle({
        title: '编辑',
      })
      this.getApplyInfo();
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  // 获取渠道申请详情
  getApplyInfo() {
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    app.ajax({
      url: 'User/Channel/getApplyInfo',
      data: {
        user_id: user_id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          var enclosure = [];
          res.data.data.enclosure.forEach(item => {
            enclosure.push(item.url);
          });
          that.setData({
            apply_id: res.data.data.id, // 渠道申请id
            name: res.data.data.name, // 单位名称
            contact: res.data.data.contact, // 联系人
            tellphone: res.data.data.tellphone, // 联系电话
            brief: res.data.data.brief, // 简介
            enclosure: enclosure, // 附件
          })
        }
      }
    })
  },
  // 单位名称
  nameInput(e) {
    this.setData({
      name: e.detail.value
    })
  },
  // 联系人
  contactInput(e) {
    this.setData({
      contact: e.detail.value
    })
  },
  // 联系电话
  tellphoneInput(e) {
    this.setData({
      tellphone: e.detail.value
    })
  },
  // 情况简介
  briefInput(e) {
    this.setData({
      brief: e.detail.value
    })
  },
  // 显示中间弹窗页
  openChoose() {
    this.setData({
      isShow: true
    })
  },
  // 关闭中间弹窗页
  closeChoose() {
    this.setData({
      isShow: false
    })
  },
  // 上传图片
  uploadImg(e) {
    var that = this;
    that.setData({
      isShow: false,
    })
    var type = e.currentTarget.dataset.type;
    var sourceType = '';
    if (type == 1) {
      sourceType = 'album';
    } else if (type == 2) {
      sourceType = 'camera';
    }
    var count = 3 - that.data.enclosure.length;
    wx.chooseImage({
      count: count,
      sizeType: ['compressed'],
      sourceType: [sourceType],
      success: function (res) {
        const tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '上传中',
        })
        that.uploadImageArr(tempFilePaths);
      },
    })
  },
  // 上传产品图 -多图
  uploadImageArr(tempFilePaths) {
    var that = this;
    var enclosure = that.data.enclosure;
    for (var i in tempFilePaths) {
      wx.uploadFile({
        url: app.globalData._url + 'Common/Common/upload',
        filePath: tempFilePaths[i],
        name: 'file',
        success: function(result) {
          if (result.statusCode !== 200) {
            wx.hideLoading();
            app.showToast('上传失败,请重新上传');
            return
          }
          var data = JSON.parse(result.data);
          if (data.code == 1000) {
            enclosure.push(data.data.url);
            that.setData({
              enclosure: enclosure
            })
            wx.hideLoading();
          } else {
            wx.hideLoading();
            app.showToast('上传失败,请重新上传');
          }
          if (i == tempFilePaths.length - 1) {
            wx.hideLoading();
          }
        },
        fail: function (fail) {
          wx.hideLoading();
          app.showToast('上传失败,请重新上传');
        }
      })
    }

  },
  // 删除附件图
  removeImg(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var enclosure = that.data.enclosure;
    enclosure.splice(index, 1);
    that.setData({
      enclosure: enclosure
    })
  },
  // 提交申请
  submit() {
    var that = this;
    if(!that.data.name){
      app.showToast('请输入单位名称', "none", 2000, function () { });
      return false;
    }else if(!that.data.contact){
      app.showToast('请输入联系人', "none", 2000, function () { });
      return false;
    }else if(!that.data.tellphone){
      app.showToast('请输入联系电话', "none", 2000, function () { });
      return false;
    }
    // var numreg = /^[1][3-9][\d]{9}$/;
    // if (numreg.test(that.data.tellphone)==false){
    //   app.showToast("请输入正确的联系电话", "none", 2000, function () { });
    //   return false;
    // }
    var user_id = wx.getStorageSync('user_id');
    var _url = '';
    if(that.data.apply_id){
      _url = 'User/Channel/editApply';
    }else{
      _url = 'User/Channel/addApply';
    }
    app.ajax({
      url: _url,
      data: {
        user_id: user_id,
        apply_id: that.data.apply_id,
        name: that.data.name, // 单位名称
        contact: that.data.contact, // 联系人
        tellphone: that.data.tellphone, // 联系电话
        brief: that.data.brief, // 简介
        enclosure: that.data.enclosure, // 附件
      },
      success: function (res) {
        if (res.data.code == 1000) {
          wx.navigateTo({
            url: '/pages/channel/applyResult/applyResult',
          })
        } else {
          app.showToast(res.data.message, "none", 2000, function () { });
        }
      }
    })
  },
})